package com.yangyang.androidconnect.controller;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.yangyang.androidconnect.common.ApiResult;
import com.yangyang.androidconnect.vo.response.StoragePaths;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RestController;

/**
 * 文件路径控制器，提供设备存储路径查询功能
 */
@RestController
@RequestMapping(path = "/api")
public class StoragePathController {
    private static final String TAG = "StoragePathController";

    /**
     * 获取设备上所有标准存储路径
     *
     * @param context Android上下文
     * @return 包含成功状态和路径数据的ApiResult对象
     */
    @GetMapping("/getAllPaths")
    public ApiResult getAllPaths(Context context) {
        try {
            StoragePaths paths = new StoragePaths();

            Log.d(TAG, "Setting internal storage paths");
            setInternalStoragePaths(context, paths);

            if (isExternalStorageAvailable()) {
                Log.d(TAG, "Setting external storage paths");
                setExternalStoragePaths(paths);
            }

            //paths.setAllStoragePaths(getAllStoragePaths());
            return ApiResult.ok(paths);
        } catch (Exception e) {
            Log.e(TAG, "Error getting storage paths", e);
            return ApiResult.error("获取路径失败: " + e.getMessage());
        }
    }

    //获取内部存储路径
    @GetMapping("/get-storage-path")
    public ApiResult getStoragePath() {
        return ApiResult.ok(Environment.getExternalStorageDirectory().getAbsolutePath());
    }

    private void setInternalStoragePaths(Context context, StoragePaths paths) {
        paths.setInternalStoragePath(context.getFilesDir().getParentFile().getAbsolutePath());
        paths.setAppFilesPath(context.getFilesDir().getAbsolutePath());
        paths.setAppCachePath(context.getCacheDir().getAbsolutePath());
    }

    private void setExternalStoragePaths(StoragePaths paths) {
        paths.setExternalStoragePath(Environment.getExternalStorageDirectory().getAbsolutePath());
        paths.setDownloadsPath(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
        paths.setDocumentsPath(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS).getAbsolutePath());
        paths.setPicturesPath(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).getAbsolutePath());
        paths.setMoviesPath(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES).getAbsolutePath());
        paths.setMusicPath(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MUSIC).getAbsolutePath());
        paths.setDcimPath(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).getAbsolutePath());
    }

    private boolean isExternalStorageAvailable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    private String[] getAllStoragePaths() {
        // 实现获取所有存储设备路径的逻辑
        return new String[]{};
    }
}